package com.airpi.connectivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.airpi.activities.LoginActivity;

/**
 * Class handling the streaming in the Android Application
 */
public class Streaming {
	
	//Initialisation of the Connection properties
	private String lineEnd = "\r\n"; //new line
	private String twoHyphens = "--";
	private String boundary =  "^$@fzjeqnf58496ef**";
	private String userID;
	private String ipServ;
	private String result;
	
	private File file;


	/**
	 * Streaming constructor
	 * @param activity The activity which allow to stream a file
	 * @param file The file to send
	 * @param ipServ The server's IP address
	 */
	public Streaming(FragmentActivity activity, File file, String ipServ) {
		this.file = file;
		this.ipServ = ipServ;
		
		SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity); 
		userID = sharedPrefs.getString(LoginActivity.USER_ID, null);
	}

	/**
	 * Send the HTTP request to the server, including file content
	 */
	public void stream() {
		if (this.file != null) {
			Log.i("Streaming", file.getName()+", path : " + file.getAbsolutePath());
			try {
				Socket socket = new Socket(ipServ,8081);
				
				OutputStream output = socket.getOutputStream();
				PrintWriter pw = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
	
				pw.println("POST /" + userID + " HTTP/1.1");
				pw.println("Connection: keep-alive");
				pw.println("Accept-Encoding: gzip, deflate");
				pw.println("Content-Length: " + String.valueOf(file.length())); System.out.println("Content-Length: " + String.valueOf(file.length()));
				pw.println("Content-Type: multipart/form-data; boundary=" + twoHyphens + boundary + lineEnd + lineEnd + twoHyphens + boundary);
				pw.println("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + lineEnd);
				
				FileInputStream fileInputStream = new FileInputStream(file);
		    	byte[] buffer = new byte[10240];
		    	int n = -1;
		    	while((n = fileInputStream.read(buffer))!=-1) {
		    		System.out.println(n);
		    		output.write(buffer,0,n);
		    		output.flush();
		    	}
		    	
			    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String inputLine;
				
				while ((inputLine = in.readLine()) != null) {
					Log.i("response", inputLine);
					if (inputLine.contains("result")) 
						this.result = inputLine;
				}
				
				if (in != null)
					in.close();
				if (fileInputStream != null)
		    		fileInputStream.close();
				if (pw != null)
					pw.close();
				if (socket != null)
					socket.close();
				
				Log.i("Streaming","envoyé");
			   
			} catch (IOException e) {
				Log.e("Streaming", "Problem with server communication");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Return the result sent by the server
	 * @return The server's answer
	 */
	public String getResult() {	
			return this.result;
	}
	
}
